package core.view
{
	import core.Main;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import org.ais.event.TEvent;
	import org.aisy.utimer.UTimer;

	public class ProgressView0 extends VUSprite
	{
		static protected var instance:ProgressView0;
		/**
		 * 背景
		 */
		protected var bgMc:MovieClip;
		/**
		 * 时间
		 */
		protected var _timeMc:MovieClip;
		/**
		 * 加载进度组
		 */
		protected var _pGroup:VUSprite;
		/**
		 * 定时器
		 */
		protected var _utimer:UTimer;
		
		public function ProgressView0()
		{
			init();
		}
		
		protected function init():void
		{
			this.buttonMode = true;
			
			bgMc = new SWF_PROGRESS_BG0();
			this.addChild(bgMc);
			
			_timeMc = new SWF_PROGRESS_TIME();
			_timeMc.txt_0.text = "00:00:00";
			_timeMc.txt_1.text = "/ 00:00:00"
			this.addChild(_timeMc);
			_timeMc.y -= height;
			
			_pGroup = new VUSprite();
			_pGroup.x = 1;
			
			this.addChild(_pGroup);
			
			__addEvent();
		}
		
		/**
		 * 
		 * 注册事件
		 * 
		 */
		protected function __addEvent():void
		{
			TEvent.newTrigger(VideoView.getInstance().getNetStream().NAME, __triggerHandler);
		}
		
		/**
		 * 
		 * 移除事件
		 * 
		 */
		protected function __removeEvent():void
		{
			_pGroup.removeEventListener(MouseEvent.MOUSE_DOWN, __pGroupHandler);
			
			TEvent.removeTrigger(VideoView.getInstance().getNetStream().NAME, __triggerHandler);
		}
		
		/**
		 * 
		 * 时间格式化
		 * @param t
		 * @return 
		 * 
		 */
		protected function timeFormat(t:*):String
		{
			if (t != undefined && !isNaN(t)) {
				var a1:*, a2:*, a3:*, t1:*, t2:*, t3:*;
				a1 = Math.floor(t / 3600);
				t1 = a1 < 10 ? ("0" + String(a1) + ":") : (String(a1) + ":");
				a2 = Math.floor((t - a1 * 60 * 60) / 60);
				a3 = Math.round((t - a1 * 60 * 60 - a2 * 60));
				if (a2 == 60) {
					a2 = 0;
					++a1;
				}
				if (a3 == 60) {
					a3 = 0;
					++a2;
					if (a2 == 60) {
						a2 = 0;
						++a1;
					}
				}
				t2 = a2 < 10 ? ("0" + String(a2) + ":") : (String(a2) + ":");
				t3 = a3 < 10 ? ("0" + String(a3)) : (String(a3));
				return (t1 + t2 + t3);
			}
			else {
				return ("00:00:00");
			}
		}
		
		/**
		 * 
		 * _pGroup 鼠标侦听
		 * @param e
		 * 
		 */
		protected function __pGroupHandler(e:MouseEvent):void
		{
			if (e.target !== _pGroup) return;
			
			VideoView.getInstance().getNetStream().seekIndex = 0;
			VideoView.getInstance().getNetStream().seekByPoint(e.localX / _pGroup.width, 2);
			e = null;
		}
		
		/**
		 * 
		 * UNetStreamy 全局侦听
		 * @param type
		 * @param data
		 * 
		 */
		protected function __triggerHandler(type:String, data:* = null):void
		{
			switch (type) {
				case "ON_METADATA":
					_pGroup.removeEventListener(MouseEvent.MOUSE_DOWN, __pGroupHandler);
					if (Main.IS.isLocal === false) _pGroup.addEventListener(MouseEvent.MOUSE_DOWN, __pGroupHandler);
					
					if (null !== _utimer) _utimer.clear();
					_utimer = new UTimer();
					_utimer.setDelay(1000);
					_utimer.setTimer(__uTimerHandler);
					_utimer.start();
					break;
				case "NEW_STREAM":
					var p1:ProgressView1 = new ProgressView1(data.index);
					p1.x = VideoView.getInstance().getNetStream().getPlayProgress() * _pGroup.width;
					_pGroup.addChild(p1);
					p1.updateView();
					p1 = null;
					break;
				case "CLEAR_PROGRESS_ALL":
					if (null !== _utimer) _utimer.clear();
					_timeMc.txt_0.text = "00:00:00";
					_timeMc.txt_1.text = "/ 00:00:00"
					_pGroup.removeEventListener(MouseEvent.MOUSE_DOWN, __pGroupHandler);
					TEvent.removeTrigger(VideoView.getInstance().getNetStream().NAME, __triggerHandler);
					TEvent.newTrigger(VideoView.getInstance().getNetStream().NAME, __triggerHandler);
					break;
			}
			type = null;
			data = null;
		}
		
		/**
		 * 
		 * 定时器侦听
		 * 
		 */
		protected function __uTimerHandler():void
		{
			if (!VideoView.getInstance().getNetStream().getMetaData()) return;
			_timeMc.txt_0.text = timeFormat(VideoView.getInstance().getNetStream().getTime());
			_timeMc.txt_1.text = "/ " + timeFormat(VideoView.getInstance().getNetStream().getMetaData().duration);
		}
		
		/**
		 * 
		 * 重置大小
		 * @param w
		 * @param h
		 * 
		 */
		override public function setSize(w:Number, h:Number):void
		{
			var _x0:Number = 0;
			var _x1:Number = 0;
			var _w:Number = w - _x0 - _x1;
			if (_pGroup.width !== 0) {
				var _sx:Number = (_w - 2) / _pGroup.width;
				var i:uint = _pGroup.numChildren;
				var obj:*;
				while (i > 0) {
					i--;
					obj = _pGroup.getChildAt(i);
					obj.x *= _sx;
					obj.setSize(_sx, h);
				}
			}
			_pGroup.graphics.clear();
			_pGroup.graphics.beginFill(0xff0000, 0);
			_pGroup.graphics.drawRect(0, 0, _w - 2, bgMc.height);
			_pGroup.graphics.endFill();
			
			bgMc.width = _w;
			_timeMc.x = _w - _timeMc.width;
			x = _x0;
			y = h - bgMc.height - ToolBarView.getInstance().height;
		}
		
		/**
		 * 
		 * 清空
		 * 
		 */
		override public function clear():void
		{
			if (null !== _utimer) _utimer.clear();
			__removeEvent();
			_utimer = null;
			bgMc = null;
			_timeMc = null;
			_pGroup = null;
			instance = null;
			super.clear();
		}
		
		static public function getInstance():ProgressView0
		{
			if (null === instance) instance = new ProgressView0();
			return instance;
		}
		
	}
}